#include "chathistory.h"
#include "utils/xio.h"

ChatHistory::ChatHistory()
{
    
}

const char* ChatHistory::DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss";

void ChatHistory::record(
    const QString& question, const QString& answer, bool newDateTime
) {
    if (newDateTime && this->questionList.length() == 0)
        this->dateTime = QDateTime::currentDateTime();
    this->questionList.push_back(question);
    this->answerList.push_back(answer);
}

bool ChatHistory::record(
    const QStringList& questionList,
    const QStringList& answerList,
    bool newDate
) {
    int questionNumber = questionList.length();
    if (questionNumber == 0)
        return false;
    if (questionNumber != answerList.length())
        return false;
    if (newDate && this->questionList.length() == 0)
        this->dateTime = QDateTime::currentDateTime();
    for (int i = 0; i < questionNumber; ++ i) {
        this->questionList.push_back(questionList.at(i));
        this->answerList.push_back(answerList.at(i));
    }
    return true;
}

void ChatHistory::find(const QString& keyword, QVector<int>& indexVector) {
    indexVector.clear();
    int questionNumber = this->getRecordsNumber();
    for (int i = 0; i < questionNumber; ++ i) {
        if (this->questionList.at(i).indexOf(keyword) != -1) {
            indexVector.push_back(i);
            continue;
        }
        if (this->answerList.at(i).indexOf(keyword) != -1)
            indexVector.push_back(i);
    }
}

int ChatHistory::getRecordsNumber() const {
    return this->questionList.length();
}

bool ChatHistory::read(int index, QString& question, QString& answer) const {
    if (index >= this->getRecordsNumber())
        return false;
    question = this->questionList.at(index);
    answer = this->answerList.at(index);
    return true;
}
    
void ChatHistory::read(QStringList& questionList, QStringList& answerList) const {
    questionList.clear();
    answerList.clear();
    int num = this->getRecordsNumber();
    for (int i = 0; i < num; ++ i) {
        questionList.push_back(this->questionList.at(i));
        answerList.push_back(this->answerList.at(i));
    }
}

bool ChatHistory::save(const QString& currentDirPath, QString& dateStr) const {
    QString historyPath;
    if (dateStr.length() == 0) {
        dateStr = this->dateTime.toString(this->DATE_FORMAT);
        historyPath = QString("%1/%2").arg(currentDirPath, dateStr);
    } else {
        if (QDateTime::fromString(dateStr, this->DATE_FORMAT).isValid()) {
            historyPath = QString("%1/%2").arg(currentDirPath, dateStr);
        } else {
            dateStr = this->dateTime.toString(this->DATE_FORMAT);
            historyPath = QString("%1/%2").arg(currentDirPath, dateStr);
        }
    }
    int cnt = this->questionList.length();
    if (QFileInfo(historyPath).isDir()) {
        for (int i = 0; i < cnt; ++ i) {
            QString questionSavePath = QString("%1/%2.question").arg(historyPath).arg(i);
            QString answerSavePath = QString("%1/%2.answer").arg(historyPath).arg(i);
            if (!QFileInfo(questionSavePath).isFile())
                XIO::writeText(this->questionList.at(i), questionSavePath);
            if (!QFileInfo(answerSavePath).isFile())
                XIO::writeText(this->answerList.at(i), answerSavePath);
        }
        return true;
    }
    
    QDir dir;
    if (!dir.mkpath(historyPath))
        return false;
    for (int i = 0; i < cnt; ++ i) {
        QString questionSavePath = QString("%1/%2.question").arg(historyPath).arg(i);
        QString answerSavePath = QString("%1/%2.answer").arg(historyPath).arg(i);
        XIO::writeText(this->questionList.at(i), questionSavePath);
        XIO::writeText(this->answerList.at(i), answerSavePath);
    }
    return true;
}

bool ChatHistory::load(const QString& historyPath) {
    if (!QFileInfo(historyPath).isDir())
        return false;
    QString dirname;
    XIO::getName(historyPath, dirname);
    QDateTime dateTime = QDateTime::fromString(dirname, this->DATE_FORMAT);
    if (!dateTime.isValid())
        return false;
    this->dateTime = dateTime;
    QStringList paths;
    if (!XIO::getPaths(historyPath, paths, false))
        return false;
    int cnt = paths.length() / 2;
    QString question;
    QString answer;
    for (int i = 0; i < cnt; ++ i) {
        if (!XIO::readText(QString("%1/%2.question").arg(historyPath).arg(i), question))
            return false;
        if (!XIO::readText(QString("%1/%2.answer").arg(historyPath).arg(i), answer))
            return false;
        this->record(question, answer, false);
    }
    return true;
}

QString ChatHistory::getDateStr() const {
    return this->dateTime.toString(this->DATE_FORMAT);
}

QDateTime ChatHistory::getDateTime() const {
    return this->dateTime;
}